﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Arizu.DAL;
using Arizu.Entity;

namespace Arizu.Repository
{
    public class AirizuComments : BaseProvider
    {
        #region
        private static volatile AirizuComments instance;
        private static object lockHelper = new object();

        private AirizuComments()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static AirizuComments CreateInstance()
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    if (instance == null)
                    {
                        instance = new AirizuComments();
                    }
                }
            }
            return instance;
        }
        #endregion

        /// <summary>
        /// 插入评价信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public long InsertComments(CommentsEntity comment)
        {
            string sql = "INSERT INTO Messages(Comment,Creator,Reciver) VALUES (@Comment,@Creator,@Reciver)";

            #region parameters
            SqlParameter parm_Comment = new SqlParameter("@Comment", comment.Comment);
            SqlParameter parm_Createby = new SqlParameter("@Creator", comment.CreateBy);
            SqlParameter parm_AboutMe = new SqlParameter("@Reciver", comment.AboutMe);

            SqlParameter[] parameters = { parm_Comment, parm_Createby, parm_AboutMe };
            #endregion

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);

                comment.ID = long.Parse(ds.Tables[0].Rows[0][0].ToString());

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            return comment.ID;
        }

        /// <summary>
        /// 获取我创建的评价
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetCommentByCreator(int userId)
        {
            DataTable dt = new DataTable();
            string sql = "select Comment, Reciver from Comments where Creator=" + userId;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        /// <summary>
        /// 获取发给我的评价
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetCommentByReciver(int userId)
        {
            DataTable dt = new DataTable();
            string sql = "select Comment, Creator from Comments where Reciver=" + userId;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        public DataTable GetRoomCommentCount(long roomID)
        {
            DataTable dt = new DataTable();
            string sql = "select count(*) from Comments where RoomID=" + roomID;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

    }
}
